package com.qk.management.controller;

import com.qk.common.PageResult;
import com.qk.common.Result;
import com.qk.entity.Course;
import com.qk.management.aop.anno.LogAnno;
import com.qk.management.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 *
 * @author lyl
 * @created 2025/10/9 14:29
 * @description
 *
 */
@Slf4j
@RestController
@RequestMapping("/courses")
@SuppressWarnings("all")
public class CourseController {

    @Autowired
    private CourseService courseService;

    @GetMapping
    public Result getListByPage(@RequestParam(required = false) String name,
                                @RequestParam(required = false) Integer subject,
                                @RequestParam(required = false) Integer target,
                                @RequestParam(defaultValue = "1") Integer page,
                                @RequestParam(defaultValue = "10") Integer pageSize) {
        log.info("分页查询课程,参数:课程名称={},课程类别={},课程目标={},当前页码={},每页大小={}", name, subject, target, page, pageSize);
        PageResult<Course> pageResult = courseService.getListByPage(name, subject, target, page, pageSize);
        return Result.success(pageResult);
    }

    @LogAnno
    @PostMapping
    public Result add(@RequestBody Course course) {
        log.info("添加课程：course={}", course.toString());
        courseService.add(course);
        return Result.success();
    }

    @LogAnno
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable Integer id) {
        log.info("删除课程：id={}", id);
        courseService.delete(id);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result getCourseById(@PathVariable Integer id) {
        log.info("查询课程：id={}", id);
        Course course = courseService.getCourseById(id);
        return Result.success(course);
    }

    @LogAnno
    @PutMapping
    public Result update(@RequestBody Course course) {
        log.info("修改课程：course={}", course.toString());
        courseService.update(course);
        return Result.success();
    }

    @GetMapping("/list")
    public Result getAllList() {
        log.info("查询所有课程");
        List<Course> list = courseService.getAllList();
        return Result.success(list);
    }

    @GetMapping("/subject/{subject}")
    public Result getListBySubject(@PathVariable Integer subject) {
        log.info("查询课程类别：subject={}", subject);
        List<Course> list = courseService.getListBySubject(subject);
        return Result.success(list);
    }

}
